package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
 * @description: 随机图片验证码
 * @author: Spark
 * @date: 2020/2/24-16:17
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("randomCode")
public class RandomCodeController {
    //使用随机类
    private Random ran = new Random();

    //生成随机验证码图片
    @RequestMapping("getCode")
    public void getCode(HttpServletRequest request,HttpServletResponse response) throws IOException {

        //1)创建缓存图片指定宽和高
        int width = 90, height = 30;
        //参数：宽，高，图片模式
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //2)获取画笔对象
        Graphics graphics = img.getGraphics();
        //3)设置画笔颜色
        graphics.setColor(Color.WHITE);
        //4)填充矩形区域
        graphics.fillRect(0, 0, width, height);
        //5)从字符数组中随机得到字符
        char[] arr = {'A', 'B', 'C', 'D', 'N', 'E', 'W', 'b', 'o', 'y', '1', '2', '3', '4', '5', '6'};
        //保存生成的验证码
        StringBuffer sb = new StringBuffer();
        //6)循环4次，画4个字符
        for (int i = 0; i < 4; i++) {
            //7)设置字的颜色为随机
            graphics.setColor(getRanColor());
            //8)设置字体，大小为19
            graphics.setFont(new Font(Font.DIALOG, Font.BOLD + Font.ITALIC, 19));
            int index = ran.nextInt(arr.length);
            char c = arr[index];
            sb.append(c);
            //9)将每个字符画到图片，x增加，y不变。10+(i*20), 20
            graphics.drawString(String.valueOf(c), 10 + (i * 20), 20);
        }
        //将验证码发送给session
        request.getSession().setAttribute("code", sb.toString());
        //10)线的位置是随机的，x范围在width之中，y的范围在height之中。
        //11)画8条干扰线，每条线的颜色不同
        for (int i = 0; i < 8; i++) {
            int x1 = ran.nextInt(width);
            int y1 = ran.nextInt(height);
            int x2 = ran.nextInt(width);
            int y2 = ran.nextInt(height);
            graphics.setColor(getRanColor());
            graphics.drawLine(x1, y1, x2, y2);
        }
        //12)将缓存的图片输出到响应输出流中
        //参数：输出的图片对象, 文件格式，输出流
        ImageIO.write(img, "jpg", response.getOutputStream());
    }

    //校验验证码
    @RequestMapping("checkCode")
    public Result checkCode(@RequestParam("code") String code, HttpServletRequest request) {
        if (code == "") {
            return new Result(false, "请输入验证码!");
        }
        String rightCode = (String) request.getSession().getAttribute("code");
        System.out.println("rightCode = " + rightCode);
        //校验验证码  忽略大小写
        if (rightCode.equalsIgnoreCase(code)) {
            return new Result(true, "验证码正确!");
        } else {
            return new Result(false, "验证码错误!");
        }
    }
    //写一个方法随机获取颜色
    private Color getRanColor() {
        //每种颜色的取值是0-255
        int r = ran.nextInt(256);
        int g = ran.nextInt(256);
        int b = ran.nextInt(256);
        return new Color(r, g, b);
    }
}